﻿//内存，硬盘的利用率
using System;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WinFormsApp_GetLocalMachineInfo
{
    public partial class FormGetSystemInfo : Form
    {
        public FormGetSystemInfo()
        {
            InitializeComponent();
        }
        [DllImport("kernel32")]
        public static extern void GetWindowsDirectory(StringBuilder WinDir, int count);
        [DllImport("kernel32")]
        public static extern void GetSystemDirectory(StringBuilder SysDir, int count);
        [DllImport("kernel32")]
        public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);
        [DllImport("kernel32")]
        public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
        [DllImport("kernel32")]
        public static extern void GetSystemTime(ref SYSTEMTIME_INFO stinfo);
        //定义CPU的信息结构
        [StructLayout(LayoutKind.Sequential)]
        public struct CPU_INFO
        {
            public uint dwOemId;
            public uint dwPageSize;
            public uint lpMinimumApplicationAddress;
            public uint lpMaximumApplicationAddress;
            public uint dwActiveProcessorMask;
            public uint dwNumberOfProcessors;
            public uint dwProcessorType;
            public uint dwAllocationGranularity;
            public uint dwProcessorLevel;
            public uint dwProcessorRevision;
        }
        //定义内存的信息结构
        [StructLayout(LayoutKind.Sequential)]
        public struct MEMORY_INFO
        {
            public uint dwLength;
            public uint dwMemoryLoad;
            public uint dwTotalPhys;
            public uint dwAvailPhys;
            public uint dwTotalPageFile;
            public uint dwAvailPageFile;
            public uint dwTotalVirtual;
            public uint dwAvailVirtual;
        }
        //定义系统时间的信息结构
        [StructLayout(LayoutKind.Sequential)]
        public struct SYSTEMTIME_INFO
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMilliseconds;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Timer timer1 = new Timer();
            timer1.Interval = 1000;
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Start();
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            ShowInfo();
        }

        void ShowInfo()
        {
            //1M=1024K 1K=1024字节
            // 1KB=1024B 1MB=1024KB 1GB=1024MB 
            label1.Text = "系统信息：";
            //调用GetWindowsDirectory和GetSystemDirectory函数分别取得Windows路径和系统路径
            string strInfo = "";
            const int nChars = 128;
            StringBuilder Buff = new StringBuilder(nChars);
            GetWindowsDirectory(Buff, nChars);
            strInfo += "\n" + "Windows路径：" + Buff.ToString();
            GetSystemDirectory(Buff, nChars);
            strInfo += "\n" + " 系统路径：" + Buff.ToString();
            //调用GetSystemInfo函数获取CPU的相关信息
            CPU_INFO CpuInfo;
            CpuInfo = new CPU_INFO();
            GetSystemInfo(ref CpuInfo);
            strInfo += "\n" + "本计算机中有" + CpuInfo.dwNumberOfProcessors.ToString() + "个CPU";
            strInfo += "\n" + "CPU的类型为" + CpuInfo.dwProcessorType.ToString();
            strInfo += "\n" + "CPU等级为" + CpuInfo.dwProcessorLevel.ToString();
            strInfo += "\n" + "CPU的OEM ID为" + CpuInfo.dwOemId.ToString();
            strInfo += "\n" + "CPU中的页面大小为" + CpuInfo.dwPageSize.ToString();
            //调用GlobalMemoryStatus函数获取内存的相关信息
            MEMORY_INFO MemInfo;
            MemInfo = new MEMORY_INFO();
            GlobalMemoryStatus(ref MemInfo);
            strInfo += "\n" + MemInfo.dwMemoryLoad.ToString() + "%的内存正在使用";
            strInfo += "\n" + "物理内存共有" + ChangeByteShown(MemInfo.dwTotalPhys);//+ "字节";
            strInfo += "\n" + "可使用的物理内存有" + ChangeByteShown(MemInfo.dwAvailPhys);// + "字节";
            strInfo += "\n" + "交换文件总大小为" + ChangeByteShown(MemInfo.dwTotalPageFile);// + "字节";
            strInfo += "\n" + "尚可交换文件大小为" + ChangeByteShown(MemInfo.dwAvailPageFile);// + "字节";
            strInfo += "\n" + "总虚拟内存有" + ChangeByteShown(MemInfo.dwTotalVirtual);//+ "字节";
            strInfo += "\n" + "未用虚拟内存有" + ChangeByteShown(MemInfo.dwAvailVirtual);// +"字节";
            //调用GetSystemTime函数获取系统时间信息
            SYSTEMTIME_INFO StInfo;
            StInfo = new SYSTEMTIME_INFO();
            GetSystemTime(ref StInfo);
            strInfo += "\n" + StInfo.wYear.ToString() + "年" + StInfo.wMonth.ToString() + "月" + StInfo.wDay.ToString() + "日";
            strInfo += " " + (StInfo.wHour + 8).ToString() + "点" + StInfo.wMinute.ToString() + "分" + StInfo.wSecond.ToString() + "秒";
            label1.Text += "\n" + strInfo;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 改变字节的显示样式
        /// </summary>
        /// <param name="argInt">字节</param>
        string ChangeByteShown(uint argInt)
        {
            string str1;
            str1 = argInt / 1024 / 1024 + " MB";
            return str1;

            string strReturn;
            uint aa = 1024;//1000;//
            //1GB=1024MB, 1MB=1024KB, 1KB= 1024B= 1024字节
            uint B = 0, KB = 0, MB = 0, GB = 0;
            KB = argInt / aa;
            B = argInt % aa;
            if (KB > 0)
            {
                MB = KB / aa;
                KB = KB % aa;
                if (MB > 0)
                {
                    GB = MB / aa;
                    MB = MB % aa;
                }
            }
            strReturn = GB + " GB," + MB + " MB," + KB + " KB," + B + " B";
            return strReturn;
        }
    }
}